﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class WeaponPreviewItem : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
{
    // Start is called before the first frame update

    public GameObject m_WeaponDescriptionPanel;
    public Button m_Button;
    public Text m_NameText;
    public Text m_ShotRange;
    public Text m_NumberText;
    public Image m_Perview;
    Weapon m_Weapon;
    public CallbackWeapon m_OnWeaponChoice;
    public GameObject m_DisablePanel;

    bool m_Enabled = true;
    bool m_IsHover = false;
    float m_HoverTimeCount = 0;

    public Weapon WeaponItem {
        set { 
            m_Weapon = value;
            if(m_Weapon == null)
            {
                m_NameText.text = "none";
                m_ShotRange.text = "--";
                if(m_NumberText)
                    m_NumberText.text = "0";
            }
            else
            {
                m_Perview.sprite = Resources.Load<Sprite>(m_Weapon.Perview);
                m_Perview.preserveAspect = true;
                m_NameText.text = m_Weapon.Name;
                m_ShotRange.text = (m_Weapon.ShutRange * 100).ToString();
                if(m_NumberText)
                    m_NumberText.text = m_Weapon.m_Number.ToString();
            }
        }
        get { return m_Weapon; }
    } 

    public void SetEnabled(bool enabled)
    {
        m_Enabled = enabled;
        m_DisablePanel.SetActive(!enabled);
    }
    void Start()
    {
        m_Button.onClick.AddListener(OnSelfClicked);
    }

    // Update is called once per frame
    void Update()
    {
        if(m_IsHover)
        {
            m_HoverTimeCount += Time.deltaTime;
            if (m_HoverTimeCount > 1)
            {
                showDescritionPanel();
                m_IsHover = false;
            }
        }
        else
        {
            m_HoverTimeCount = 0;
        }

#if UNITY_ANDROID
        if (!UtilsUnity.IsOnUI(Input.mousePosition) && (Input.touchCount > 1 || Input.GetMouseButtonDown(0)))
        {
            hideDescriptionPanel();
            m_IsHover = false;
            m_HoverTimeCount = 0;
        }
#else
        if (!EventSystem.current.IsPointerOverGameObject())
        {
            hideDescriptionPanel();
            m_IsHover = false;
        }
#endif
        if (Input.mouseScrollDelta != new Vector2(0,0))
        {
            m_HoverTimeCount = 0;
        }
    }

    void OnSelfClicked()
    {
        if (!m_Enabled)
            return;
        if(null != m_OnWeaponChoice)
        {
            m_OnWeaponChoice(m_Weapon);
        }
        hideDescriptionPanel();
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
#if UNITY_ANDROID
#else
        hideDescriptionPanel();
        m_IsHover = true;
        m_HoverTimeCount = 0;
#endif
    }

    public void OnPointerExit(PointerEventData eventData)
    {
#if UNITY_ANDROID
#else
        m_HoverTimeCount = 0;
        //m_IsHover = false;
#endif
    }

    public void showDescritionPanel()
    {
        m_WeaponDescriptionPanel.GetComponent<RectTransform>().position = UtilsUnity.GetPositionInScreen(Input.mousePosition , m_WeaponDescriptionPanel.GetComponent<RectTransform>().sizeDelta);
        m_WeaponDescriptionPanel.SetActive(true);
        WeaponDescriptionPanel panel = m_WeaponDescriptionPanel.GetComponent<WeaponDescriptionPanel>();
        panel.PerviewWeapon = m_Weapon;
    }
    void hideDescriptionPanel()
    {
        if(m_WeaponDescriptionPanel.activeSelf)
            m_WeaponDescriptionPanel.SetActive(false);
    }

}
